﻿package bipede.animation
{
	import bipede.animation.AnimationTimer;
	import events.MoveEvent;
	import flash.events.TimerEvent;
	import fab.geom.Segment;
	import flash.geom.Point;
	
	public class SegmentMove extends AnimationTimer
	{
		public var segment:Segment;
		
		public function SegmentMove( _type:String )
		{
			type = _type;
			super();
		}
		override public function start():void
		{
			segment = new Segment( cfg.place, cfg.cible );
			super.start();
		}	
		override public function _tick( event:TimerEvent ):void
		{
			avancement += (sens?1: -1) * cfg.vitesse;
			trace(sens, avancement);
			if ( sens && avancement >= 1 )
			{
				dispatchEvent( new MoveEvent( MoveEvent.ENDMOVING, cfg ) );
				stop();
				return;
			}
			if ( !sens && avancement < 0 )
			{
				dispatchEvent( new MoveEvent( MoveEvent.ENDMOVING, cfg ) );
				stop();
				return;
			}			
			dispatchEvent( new MoveEvent( MoveEvent.MOVE, segment.pointAt( avancement ) ) );			
			dispatchEvent( new MoveEvent( MoveEvent.POSEPIEDGAUCHE ) );
			dispatchEvent( new MoveEvent( MoveEvent.POSEPIEDDROIT ) );
		}
	}
}